package com.kuang.utils;

import com.alibaba.fastjson.JSONObject;
import com.kuang.sync.manageup.ManageUpService;
import com.kuang.sync.pfsiup.PfsiUpService;
import com.kuang.sync.singledown.SingleDownService;
import com.kuang.sync.singleup.SingleUpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 常规MQTT回调函数
 *
 * @author zoe_zls
 * @since 2024/3/4
 */
@Slf4j
@Component
public class Callback implements MqttCallback {

    @Resource
    SingleUpService singleUpService;
    @Resource
    SingleDownService singleDownService;
    @Resource
    ManageUpService manageUpService;
    @Resource
    PfsiUpService pfsiUpService;

    //静态方法加载service/mapper
    private static Callback callback;

    @PostConstruct
    public void init() {
        callback = this;
    }

    /**
     * MQTT 断开连接会执行此方法
     */
    @Override
    public void connectionLost(Throwable throwable) {
        log.info("断开了MQTT连接 ：{}", throwable.getMessage());
        log.error(throwable.getMessage(), throwable);
    }

    /**
     * publish发布成功后会执行到这里
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.info("发布消息成功");
    }

    /**
     * subscribe订阅后得到的消息会执行到这里
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        //  TODO    此处可以将订阅得到的消息进行业务处理、数据存储
        String s = new String(message.getPayload());
        String[] split = topic.split("/");
        String id = split[3];
        //获取主题的第一层
        String topic_one = split[1];
        JSONObject inJson = JSONObject.parseObject(s);
        String cmd = inJson.getString("cmd");

        //设备端与云端协议
        if (StringUtils.equals(topic_one, "single")) {
            singleUp(topic, message, cmd, id, s, inJson);

            //管理机与云端协议
        } else if (StringUtils.equals(topic_one, "manage")) {
            manageUp(topic, message, cmd, id, s, inJson);

            //水肥一体机与云端协议
        } else if (StringUtils.equals(topic_one, "pfsi")) {
            pfsiUp(topic, message, cmd, id, s, inJson);
        }


    }


    /**
     * 设备端与云端协议
     *
     * @param topic
     * @param message
     * @param cmd
     * @param id
     * @param s
     * @param inJson
     */
    public void singleUp(String topic, MqttMessage message, String cmd, String id, String s, JSONObject inJson) {
        //1.设备端心跳 cmd=controller_heart
        if (StringUtils.equals("1", cmd)) {
            //调用service
            String runSrc = callback.singleUpService.controllerHeartSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //2.设备端注册 cmd=controller_register
        } else if (StringUtils.equals("2", cmd)) {
            //调用service
            String runSrc = callback.singleUpService.controllerRegisterSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //3.设备端开机 cmd=controller_poweron
        } else if (StringUtils.equals("3", cmd)) {
            //调用service
            String runSrc = callback.singleUpService.controllerPoweronSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //4.设设备端本地开井 cmd=local_open_well
        } else if (StringUtils.equals("4", cmd)) {
            //调用service
            String runSrc = callback.singleUpService.localOpenWellSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //5.设备端本地关井 cmd=local_close_well
        } else if (StringUtils.equals("5", cmd)) {
            //调用service
            String runSrc = callback.singleUpService.localCloseWellSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //8.设备端本地开井实时数据 cmd=local_opening_well
        } else if (StringUtils.equals("8", cmd)) {
            //调用service
            String runSrc = callback.singleUpService.localOpeningWellSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //9.设备端远程开井实时数据 cmd=remote_opening_well
        } else if (StringUtils.equals("9", cmd)) {
            //调用service
            String runSrc = callback.singleUpService.remoteOpeningWellSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //2.6 服务器远程开井 cmd=remote_open_well
        } else if (StringUtils.equals("6", cmd)) {
            //调用service
            String runSrc = callback.singleDownService.postRemoteOpenWellSrv(id, inJson);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //2.7 2.7 服务器远程关井 cmd=remote_close_well
        } else if (StringUtils.equals("7", cmd)) {
            //调用service
            String runSrc = callback.singleDownService.postRemoteCloseWellSrv(id, inJson);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //2.10 获取设备端信息 cmd=controller_info
        } else if (StringUtils.equals("10", cmd)) {
            //调用service
            String runSrc = callback.singleDownService.postControllerInfoSrv(id, inJson);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));
        }
        return;
    }

    /**
     * 管理机与云端协议
     *
     * @param topic
     * @param message
     * @param cmd
     * @param id
     * @param s
     * @param inJson
     */
    public void manageUp(String topic, MqttMessage message, String cmd, String id, String s, JSONObject inJson) {
        //1.3.2.1管理机注册
        if (StringUtils.equals("1", cmd)) {
            //调用service
            String runSrc = callback.manageUpService.manageDeviceRegisterSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //2.3.2.2用户卡开通
        } else if (StringUtils.equals("2", cmd)) {
            //调用service
            String runSrc = callback.manageUpService.userCardActivateSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //3.用户卡充值
        } else if (StringUtils.equals("3", cmd)) {
            //调用service
            String runSrc = callback.manageUpService.userCardRechargeSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //3.2.4设备端开机
        } else if (StringUtils.equals("4", cmd)) {
            //调用service
            String runSrc = callback.manageUpService.manageDeviceOpenSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //3.2.5设备端心跳
        } else if (StringUtils.equals("5", cmd)) {
            //调用service
            String runSrc = callback.manageUpService.manageDeviceHeartSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

        }

        return;
    }

    /**
     * FXT水肥一体机与云端数据通讯协议
     *
     * @param topic
     * @param message
     * @param cmd
     * @param id
     * @param s
     * @param inJson
     */
    public void pfsiUp(String topic, MqttMessage message, String cmd, String id, String s, JSONObject inJson) {
        //设备端注册
        if (StringUtils.equals("1", cmd)) {
            //1.调用service
            String runSrc = callback.pfsiUpService.pfsiDeviceRegisterSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //2.设备端开机
        } else if (StringUtils.equals("2", cmd)) {
            //调用service
            String runSrc = callback.pfsiUpService.pfsiDeviceOpenSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //3.设备状态信息定时上报
        } else if (StringUtils.equals("3", cmd)) {
            //调用service
            String runSrc = callback.pfsiUpService.pfsiDeviceStateMsgSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

            //4.肥通支路信息定时上报
        } else if (StringUtils.equals("4", cmd)) {
            //调用service
            String runSrc = callback.pfsiUpService.pfsiDeviceBranchSrv(id, s);
            log.info("收到来自 " + topic + " 的消息：{}", new String(message.getPayload()));

        }

        return;
    }

}